package qldc.pm.qlktcsdldc.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import qldc.pm.qlktcsdldc.NoSuchTruongCongDanException;
import qldc.pm.qlktcsdldc.model.TruongCongDan;
import qldc.pm.qlktcsdldc.model.impl.TruongCongDanImpl;
import qldc.pm.qlktcsdldc.model.impl.TruongCongDanModelImpl;
import qldc.pm.qlktcsdldc.service.persistence.TruongCongDanPersistence;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the truong cong dan service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see TruongCongDanPersistence
 * @see TruongCongDanUtil
 * @generated
 */
public class TruongCongDanPersistenceImpl extends BasePersistenceImpl<TruongCongDan>
    implements TruongCongDanPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link TruongCongDanUtil} to access the truong cong dan persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = TruongCongDanImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
            TruongCongDanModelImpl.FINDER_CACHE_ENABLED,
            TruongCongDanImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
            TruongCongDanModelImpl.FINDER_CACHE_ENABLED,
            TruongCongDanImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
            TruongCongDanModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_IDDMDC_TRUONGCONGDAN =
        new FinderPath(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
            TruongCongDanModelImpl.FINDER_CACHE_ENABLED,
            TruongCongDanImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findByIDDMDC_TruongCongDan",
            new String[] {
                Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDDMDC_TRUONGCONGDAN =
        new FinderPath(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
            TruongCongDanModelImpl.FINDER_CACHE_ENABLED,
            TruongCongDanImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByIDDMDC_TruongCongDan",
            new String[] { Long.class.getName() },
            TruongCongDanModelImpl.IDDMDC_TRUONGCONGDAN_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_IDDMDC_TRUONGCONGDAN = new FinderPath(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
            TruongCongDanModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByIDDMDC_TruongCongDan", new String[] { Long.class.getName() });
    private static final String _FINDER_COLUMN_IDDMDC_TRUONGCONGDAN_IDDMDC_TRUONGCONGDAN_2 =
        "truongCongDan.IDDMDC_TruongCongDan = ?";
    private static final String _SQL_SELECT_TRUONGCONGDAN = "SELECT truongCongDan FROM TruongCongDan truongCongDan";
    private static final String _SQL_SELECT_TRUONGCONGDAN_WHERE = "SELECT truongCongDan FROM TruongCongDan truongCongDan WHERE ";
    private static final String _SQL_COUNT_TRUONGCONGDAN = "SELECT COUNT(truongCongDan) FROM TruongCongDan truongCongDan";
    private static final String _SQL_COUNT_TRUONGCONGDAN_WHERE = "SELECT COUNT(truongCongDan) FROM TruongCongDan truongCongDan WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "truongCongDan.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No TruongCongDan exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No TruongCongDan exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(TruongCongDanPersistenceImpl.class);
    private static TruongCongDan _nullTruongCongDan = new TruongCongDanImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<TruongCongDan> toCacheModel() {
                return _nullTruongCongDanCacheModel;
            }
        };

    private static CacheModel<TruongCongDan> _nullTruongCongDanCacheModel = new CacheModel<TruongCongDan>() {
            @Override
            public TruongCongDan toEntityModel() {
                return _nullTruongCongDan;
            }
        };

    public TruongCongDanPersistenceImpl() {
        setModelClass(TruongCongDan.class);
    }

    /**
     * Returns all the truong cong dans where IDDMDC_TruongCongDan = &#63;.
     *
     * @param IDDMDC_TruongCongDan the i d d m d c_ truong cong dan
     * @return the matching truong cong dans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TruongCongDan> findByIDDMDC_TruongCongDan(
        long IDDMDC_TruongCongDan) throws SystemException {
        return findByIDDMDC_TruongCongDan(IDDMDC_TruongCongDan,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the truong cong dans where IDDMDC_TruongCongDan = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.TruongCongDanModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param IDDMDC_TruongCongDan the i d d m d c_ truong cong dan
     * @param start the lower bound of the range of truong cong dans
     * @param end the upper bound of the range of truong cong dans (not inclusive)
     * @return the range of matching truong cong dans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TruongCongDan> findByIDDMDC_TruongCongDan(
        long IDDMDC_TruongCongDan, int start, int end)
        throws SystemException {
        return findByIDDMDC_TruongCongDan(IDDMDC_TruongCongDan, start, end, null);
    }

    /**
     * Returns an ordered range of all the truong cong dans where IDDMDC_TruongCongDan = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.TruongCongDanModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param IDDMDC_TruongCongDan the i d d m d c_ truong cong dan
     * @param start the lower bound of the range of truong cong dans
     * @param end the upper bound of the range of truong cong dans (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching truong cong dans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TruongCongDan> findByIDDMDC_TruongCongDan(
        long IDDMDC_TruongCongDan, int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDDMDC_TRUONGCONGDAN;
            finderArgs = new Object[] { IDDMDC_TruongCongDan };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_IDDMDC_TRUONGCONGDAN;
            finderArgs = new Object[] {
                    IDDMDC_TruongCongDan,
                    
                    start, end, orderByComparator
                };
        }

        List<TruongCongDan> list = (List<TruongCongDan>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (TruongCongDan truongCongDan : list) {
                if ((IDDMDC_TruongCongDan != truongCongDan.getIDDMDC_TruongCongDan())) {
                    list = null;

                    break;
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 +
                        (orderByComparator.getOrderByFields().length * 3));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_TRUONGCONGDAN_WHERE);

            query.append(_FINDER_COLUMN_IDDMDC_TRUONGCONGDAN_IDDMDC_TRUONGCONGDAN_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);
            } else
             if (pagination) {
                query.append(TruongCongDanModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(IDDMDC_TruongCongDan);

                if (!pagination) {
                    list = (List<TruongCongDan>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<TruongCongDan>(list);
                } else {
                    list = (List<TruongCongDan>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first truong cong dan in the ordered set where IDDMDC_TruongCongDan = &#63;.
     *
     * @param IDDMDC_TruongCongDan the i d d m d c_ truong cong dan
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching truong cong dan
     * @throws qldc.pm.qlktcsdldc.NoSuchTruongCongDanException if a matching truong cong dan could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan findByIDDMDC_TruongCongDan_First(
        long IDDMDC_TruongCongDan, OrderByComparator orderByComparator)
        throws NoSuchTruongCongDanException, SystemException {
        TruongCongDan truongCongDan = fetchByIDDMDC_TruongCongDan_First(IDDMDC_TruongCongDan,
                orderByComparator);

        if (truongCongDan != null) {
            return truongCongDan;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("IDDMDC_TruongCongDan=");
        msg.append(IDDMDC_TruongCongDan);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTruongCongDanException(msg.toString());
    }

    /**
     * Returns the first truong cong dan in the ordered set where IDDMDC_TruongCongDan = &#63;.
     *
     * @param IDDMDC_TruongCongDan the i d d m d c_ truong cong dan
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching truong cong dan, or <code>null</code> if a matching truong cong dan could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan fetchByIDDMDC_TruongCongDan_First(
        long IDDMDC_TruongCongDan, OrderByComparator orderByComparator)
        throws SystemException {
        List<TruongCongDan> list = findByIDDMDC_TruongCongDan(IDDMDC_TruongCongDan,
                0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last truong cong dan in the ordered set where IDDMDC_TruongCongDan = &#63;.
     *
     * @param IDDMDC_TruongCongDan the i d d m d c_ truong cong dan
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching truong cong dan
     * @throws qldc.pm.qlktcsdldc.NoSuchTruongCongDanException if a matching truong cong dan could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan findByIDDMDC_TruongCongDan_Last(
        long IDDMDC_TruongCongDan, OrderByComparator orderByComparator)
        throws NoSuchTruongCongDanException, SystemException {
        TruongCongDan truongCongDan = fetchByIDDMDC_TruongCongDan_Last(IDDMDC_TruongCongDan,
                orderByComparator);

        if (truongCongDan != null) {
            return truongCongDan;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("IDDMDC_TruongCongDan=");
        msg.append(IDDMDC_TruongCongDan);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTruongCongDanException(msg.toString());
    }

    /**
     * Returns the last truong cong dan in the ordered set where IDDMDC_TruongCongDan = &#63;.
     *
     * @param IDDMDC_TruongCongDan the i d d m d c_ truong cong dan
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching truong cong dan, or <code>null</code> if a matching truong cong dan could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan fetchByIDDMDC_TruongCongDan_Last(
        long IDDMDC_TruongCongDan, OrderByComparator orderByComparator)
        throws SystemException {
        int count = countByIDDMDC_TruongCongDan(IDDMDC_TruongCongDan);

        if (count == 0) {
            return null;
        }

        List<TruongCongDan> list = findByIDDMDC_TruongCongDan(IDDMDC_TruongCongDan,
                count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Removes all the truong cong dans where IDDMDC_TruongCongDan = &#63; from the database.
     *
     * @param IDDMDC_TruongCongDan the i d d m d c_ truong cong dan
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByIDDMDC_TruongCongDan(long IDDMDC_TruongCongDan)
        throws SystemException {
        for (TruongCongDan truongCongDan : findByIDDMDC_TruongCongDan(
                IDDMDC_TruongCongDan, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(truongCongDan);
        }
    }

    /**
     * Returns the number of truong cong dans where IDDMDC_TruongCongDan = &#63;.
     *
     * @param IDDMDC_TruongCongDan the i d d m d c_ truong cong dan
     * @return the number of matching truong cong dans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByIDDMDC_TruongCongDan(long IDDMDC_TruongCongDan)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_IDDMDC_TRUONGCONGDAN;

        Object[] finderArgs = new Object[] { IDDMDC_TruongCongDan };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_TRUONGCONGDAN_WHERE);

            query.append(_FINDER_COLUMN_IDDMDC_TRUONGCONGDAN_IDDMDC_TRUONGCONGDAN_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(IDDMDC_TruongCongDan);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Caches the truong cong dan in the entity cache if it is enabled.
     *
     * @param truongCongDan the truong cong dan
     */
    @Override
    public void cacheResult(TruongCongDan truongCongDan) {
        EntityCacheUtil.putResult(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
            TruongCongDanImpl.class, truongCongDan.getPrimaryKey(),
            truongCongDan);

        truongCongDan.resetOriginalValues();
    }

    /**
     * Caches the truong cong dans in the entity cache if it is enabled.
     *
     * @param truongCongDans the truong cong dans
     */
    @Override
    public void cacheResult(List<TruongCongDan> truongCongDans) {
        for (TruongCongDan truongCongDan : truongCongDans) {
            if (EntityCacheUtil.getResult(
                        TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
                        TruongCongDanImpl.class, truongCongDan.getPrimaryKey()) == null) {
                cacheResult(truongCongDan);
            } else {
                truongCongDan.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all truong cong dans.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(TruongCongDanImpl.class.getName());
        }

        EntityCacheUtil.clearCache(TruongCongDanImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the truong cong dan.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(TruongCongDan truongCongDan) {
        EntityCacheUtil.removeResult(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
            TruongCongDanImpl.class, truongCongDan.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    @Override
    public void clearCache(List<TruongCongDan> truongCongDans) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (TruongCongDan truongCongDan : truongCongDans) {
            EntityCacheUtil.removeResult(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
                TruongCongDanImpl.class, truongCongDan.getPrimaryKey());
        }
    }

    /**
     * Creates a new truong cong dan with the primary key. Does not add the truong cong dan to the database.
     *
     * @param IDDMDC_TruongCongDan the primary key for the new truong cong dan
     * @return the new truong cong dan
     */
    @Override
    public TruongCongDan create(long IDDMDC_TruongCongDan) {
        TruongCongDan truongCongDan = new TruongCongDanImpl();

        truongCongDan.setNew(true);
        truongCongDan.setPrimaryKey(IDDMDC_TruongCongDan);

        return truongCongDan;
    }

    /**
     * Removes the truong cong dan with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param IDDMDC_TruongCongDan the primary key of the truong cong dan
     * @return the truong cong dan that was removed
     * @throws qldc.pm.qlktcsdldc.NoSuchTruongCongDanException if a truong cong dan with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan remove(long IDDMDC_TruongCongDan)
        throws NoSuchTruongCongDanException, SystemException {
        return remove((Serializable) IDDMDC_TruongCongDan);
    }

    /**
     * Removes the truong cong dan with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the truong cong dan
     * @return the truong cong dan that was removed
     * @throws qldc.pm.qlktcsdldc.NoSuchTruongCongDanException if a truong cong dan with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan remove(Serializable primaryKey)
        throws NoSuchTruongCongDanException, SystemException {
        Session session = null;

        try {
            session = openSession();

            TruongCongDan truongCongDan = (TruongCongDan) session.get(TruongCongDanImpl.class,
                    primaryKey);

            if (truongCongDan == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchTruongCongDanException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

            return remove(truongCongDan);
        } catch (NoSuchTruongCongDanException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected TruongCongDan removeImpl(TruongCongDan truongCongDan)
        throws SystemException {
        truongCongDan = toUnwrappedModel(truongCongDan);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(truongCongDan)) {
                truongCongDan = (TruongCongDan) session.get(TruongCongDanImpl.class,
                        truongCongDan.getPrimaryKeyObj());
            }

            if (truongCongDan != null) {
                session.delete(truongCongDan);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (truongCongDan != null) {
            clearCache(truongCongDan);
        }

        return truongCongDan;
    }

    @Override
    public TruongCongDan updateImpl(
        qldc.pm.qlktcsdldc.model.TruongCongDan truongCongDan)
        throws SystemException {
        truongCongDan = toUnwrappedModel(truongCongDan);

        boolean isNew = truongCongDan.isNew();

        TruongCongDanModelImpl truongCongDanModelImpl = (TruongCongDanModelImpl) truongCongDan;

        Session session = null;

        try {
            session = openSession();

            if (truongCongDan.isNew()) {
                session.save(truongCongDan);

                truongCongDan.setNew(false);
            } else {
                session.merge(truongCongDan);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !TruongCongDanModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }
        else {
            if ((truongCongDanModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDDMDC_TRUONGCONGDAN.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        truongCongDanModelImpl.getOriginalIDDMDC_TruongCongDan()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_IDDMDC_TRUONGCONGDAN,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDDMDC_TRUONGCONGDAN,
                    args);

                args = new Object[] {
                        truongCongDanModelImpl.getIDDMDC_TruongCongDan()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_IDDMDC_TRUONGCONGDAN,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDDMDC_TRUONGCONGDAN,
                    args);
            }
        }

        EntityCacheUtil.putResult(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
            TruongCongDanImpl.class, truongCongDan.getPrimaryKey(),
            truongCongDan);

        return truongCongDan;
    }

    protected TruongCongDan toUnwrappedModel(TruongCongDan truongCongDan) {
        if (truongCongDan instanceof TruongCongDanImpl) {
            return truongCongDan;
        }

        TruongCongDanImpl truongCongDanImpl = new TruongCongDanImpl();

        truongCongDanImpl.setNew(truongCongDan.isNew());
        truongCongDanImpl.setPrimaryKey(truongCongDan.getPrimaryKey());

        truongCongDanImpl.setIDDMDC_TruongCongDan(truongCongDan.getIDDMDC_TruongCongDan());
        truongCongDanImpl.setGroupId(truongCongDan.getGroupId());
        truongCongDanImpl.setCompanyId(truongCongDan.getCompanyId());
        truongCongDanImpl.setUserId(truongCongDan.getUserId());
        truongCongDanImpl.setUserName(truongCongDan.getUserName());
        truongCongDanImpl.setCreateDate(truongCongDan.getCreateDate());
        truongCongDanImpl.setModifiedDate(truongCongDan.getModifiedDate());
        truongCongDanImpl.setTenTruongCongDan(truongCongDan.getTenTruongCongDan());
        truongCongDanImpl.setGiaiThich(truongCongDan.getGiaiThich());

        return truongCongDanImpl;
    }

    /**
     * Returns the truong cong dan with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the truong cong dan
     * @return the truong cong dan
     * @throws qldc.pm.qlktcsdldc.NoSuchTruongCongDanException if a truong cong dan with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan findByPrimaryKey(Serializable primaryKey)
        throws NoSuchTruongCongDanException, SystemException {
        TruongCongDan truongCongDan = fetchByPrimaryKey(primaryKey);

        if (truongCongDan == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchTruongCongDanException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return truongCongDan;
    }

    /**
     * Returns the truong cong dan with the primary key or throws a {@link qldc.pm.qlktcsdldc.NoSuchTruongCongDanException} if it could not be found.
     *
     * @param IDDMDC_TruongCongDan the primary key of the truong cong dan
     * @return the truong cong dan
     * @throws qldc.pm.qlktcsdldc.NoSuchTruongCongDanException if a truong cong dan with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan findByPrimaryKey(long IDDMDC_TruongCongDan)
        throws NoSuchTruongCongDanException, SystemException {
        return findByPrimaryKey((Serializable) IDDMDC_TruongCongDan);
    }

    /**
     * Returns the truong cong dan with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the truong cong dan
     * @return the truong cong dan, or <code>null</code> if a truong cong dan with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        TruongCongDan truongCongDan = (TruongCongDan) EntityCacheUtil.getResult(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
                TruongCongDanImpl.class, primaryKey);

        if (truongCongDan == _nullTruongCongDan) {
            return null;
        }

        if (truongCongDan == null) {
            Session session = null;

            try {
                session = openSession();

                truongCongDan = (TruongCongDan) session.get(TruongCongDanImpl.class,
                        primaryKey);

                if (truongCongDan != null) {
                    cacheResult(truongCongDan);
                } else {
                    EntityCacheUtil.putResult(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
                        TruongCongDanImpl.class, primaryKey, _nullTruongCongDan);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(TruongCongDanModelImpl.ENTITY_CACHE_ENABLED,
                    TruongCongDanImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return truongCongDan;
    }

    /**
     * Returns the truong cong dan with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param IDDMDC_TruongCongDan the primary key of the truong cong dan
     * @return the truong cong dan, or <code>null</code> if a truong cong dan with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public TruongCongDan fetchByPrimaryKey(long IDDMDC_TruongCongDan)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) IDDMDC_TruongCongDan);
    }

    /**
     * Returns all the truong cong dans.
     *
     * @return the truong cong dans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TruongCongDan> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the truong cong dans.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.TruongCongDanModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of truong cong dans
     * @param end the upper bound of the range of truong cong dans (not inclusive)
     * @return the range of truong cong dans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TruongCongDan> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the truong cong dans.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.TruongCongDanModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of truong cong dans
     * @param end the upper bound of the range of truong cong dans (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of truong cong dans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<TruongCongDan> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<TruongCongDan> list = (List<TruongCongDan>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 +
                        (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_TRUONGCONGDAN);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_TRUONGCONGDAN;

                if (pagination) {
                    sql = sql.concat(TruongCongDanModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<TruongCongDan>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<TruongCongDan>(list);
                } else {
                    list = (List<TruongCongDan>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the truong cong dans from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (TruongCongDan truongCongDan : findAll()) {
            remove(truongCongDan);
        }
    }

    /**
     * Returns the number of truong cong dans.
     *
     * @return the number of truong cong dans
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_TRUONGCONGDAN);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the truong cong dan persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.qldc.pm.qlktcsdldc.model.TruongCongDan")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<TruongCongDan>> listenersList = new ArrayList<ModelListener<TruongCongDan>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<TruongCongDan>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(TruongCongDanImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
